package com.xccvt.aspect;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        // 创建Spring应用上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        
        // 获取UserService Bean
        UserService userService = context.getBean(UserService.class);
        
        System.out.println("======= 正常执行方法 =======");
        try {
            String user = userService.getUserById(1);
            System.out.println("获取用户结果: " + user);
        } catch (Exception e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
        
        System.out.println("\n======= 执行带参数的方法 =======");
        try {
            userService.saveUser("张三");
        } catch (Exception e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
        
        System.out.println("\n======= this和target区别演示 =======");
        try {
            userService.demonstrateThisVsTarget();
        } catch (Exception e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
        
        System.out.println("\n======= 执行抛出异常的方法 =======");
        try {
            String user = userService.getUserById(-1);
            System.out.println("获取用户结果: " + user);
        } catch (Exception e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
        
        System.out.println("\n======= 执行另一个抛出异常的方法 =======");
        try {
            userService.saveUser("");
        } catch (Exception e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
        
        // 关闭上下文
        context.close();
    }
}